package com.huluobo.a2207douyindemo.mine;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.base.bean.CommonBeanResult;
import com.huluobo.base.bean.VideoBeanResult;
import com.huluobo.base.mvvm.BaseRepo;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2024/9/20.
 */
public class MineRepo extends BaseRepo {
    public void getLikeVideos(MutableLiveData<VideoBeanResult> success, MutableLiveData<String> failed) {
        apiService.getLikeVideos()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        success.setValue(videoBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void noLikeVideo(int videoId, MutableLiveData<CommonBeanResult> success, MutableLiveData<String> failed) {
        apiService.noLikeVideo(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonBeanResult commonBeanResult) {
                        success.setValue(commonBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getCollectVideos(MutableLiveData<List<VideoBeanResult.VideoBean>> success,
                                 MutableLiveData<String> failed) {
        videoDao.queryVideoInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<VideoBeanResult.VideoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<VideoBeanResult.VideoBean> videoBeans) {
                        success.setValue(videoBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void deleteCollectVideo(VideoBeanResult.VideoBean videoBean, MutableLiveData<String> success,
                                   MutableLiveData<String> failed) {
        try {
            videoDao.deleteVideoInfo(videoBean);
            success.setValue("删除收藏成功");
        } catch (Exception e) {
            failed.setValue(e.getMessage());
        }
    }
}
